import { state } from "../../config.js";
import { randomColor } from "../../utils/randomColor.js";
import { box, currentState, domg, role1, role2 } from "../../index.js";
import { getPosition } from "../newTransfrom.js";
import { emit, scoket } from "../../utils/scoket.js";
import { collide } from "./collide.js";
import { allRolePosition } from "../../role.js";
var createBullet = function (point, direction, preState) {
    //如果这个子弹是自己触发的就用自己的state如果不是就用服务器的state
    var cState = preState || state;
    var bullel = document.createElement("div");
    bullel.classList.add("bullet");
    bullel.style.transform = "translate(".concat(point.x, "px,").concat(point.y, "px)");
    bullel.style.backgroundColor = randomColor();
    var timer = -1;
    domg("#app").appendChild(bullel);
    var bulletState = {
        h: bullel.offsetHeight,
        w: bullel.offsetWidth
    };
    //#region 开启定时器每秒移动并判断情况
    timer = setInterval(function () {
        var position = getPosition(bullel);
        //判断是否进入边界
        if (position.x + 10 > box.boxW || position.x - 10 < 0) {
            position.x = box.boxW;
            bullel.remove();
            clearInterval(timer);
        }
        //#region 是否击中判断
        position.x += cState.bulletSpeed * direction;
        var bulletinfo = {
            x: position.x,
            y: position.y,
            w: bulletState.w,
            h: bulletState.h
        };
        var roleInfo;
        if ((preState === null || preState === void 0 ? void 0 : preState.role) === "p2" || (!preState && currentState.role === "p2")) {
            roleInfo = allRolePosition.p1;
        }
        if ((preState === null || preState === void 0 ? void 0 : preState.role) === "p1" || (!preState && currentState.role === "p1")) {
            roleInfo = allRolePosition.p2;
        }
        collide(bulletinfo, roleInfo, function () {
            bullel.remove();
            clearInterval(timer);
            if (preState)
                return;
            if (currentState.role === "p1") {
                scoket.$emit({ role: "p2", path: "/hp", value: { hp: -1 } });
            }
            if (currentState.role === "p2") {
                scoket.$emit({ role: "p1", path: "/hp", value: { hp: -1 } });
            }
        });
        //#endregion
        bullel.style.transform = "translate(".concat(position.x, "px,").concat(position.y, "px)");
    }, 1000 / state.bulletMove);
    //#endregion
};
//监听来自服务器射击的响应
scoket.$on(function (res) {
    if (res.path === "/fire/j") {
        var val = res.value;
        val.role = res.role;
        if (res.role === "p1") {
            //收到响应角色来自p1就把当前子弹射击方向改为正方向
            val.direction = 1;
            fire(role1, val);
        }
        if (res.role === "p2") {
            //收到响应角色来自p2就把当前子弹射击方向改为反方向
            val.direction = -1;
            fire(role2, val);
        }
    }
});
//掉血操作
scoket.$on(function (_a) {
    var path = _a.path, role = _a.role, value = _a.value.value;
    if (path !== "/hp")
        return;
    if (role === currentState.role && role === "p1") {
        state.hp += value.hp;
        emit.p1("/getState", state);
    }
    if (role === currentState.role && role === "p2") {
        state.hp += value.hp;
        emit.p2("/getState", state);
    }
});
var timer;
/**
 *
 * @param ele 子弹来自那个角色
 * @param _dire 可选参数,如果有这个参数就说明是来自服务器,如果没有就是来自玩家自己
 * @returns
 */
export var fire = function (ele, _dire) {
    var direction;
    if (_dire) {
        direction = _dire.direction;
    }
    else {
        direction = currentState.role === "p1" ? 1 : -1;
    }
    //及时响应操作
    if ((_dire === null || _dire === void 0 ? void 0 : _dire.role) === currentState.role)
        return;
    if (!_dire)
        if (typeof timer === "number")
            return;
    //判断这个函数是玩家调用的还是服务器返回的来决定是否发送请求
    _dire ||
        scoket.$emit({
            role: currentState.role,
            path: "/fire/j",
            value: state
        });
    //#region 创建子弹
    var rolePosition = getPosition(ele);
    var firePoint = {
        //子弹需要生成的位置
        x: rolePosition.x +
            direction * allRolePosition[currentState.role].w,
        y: rolePosition.y + allRolePosition[currentState.role].h / 2
    };
    createBullet(firePoint, direction, _dire);
    //#endregion
    //控制子弹发射频率
    var frequency;
    if (_dire) {
        frequency = _dire.bulletFrequency;
    }
    else {
        frequency = state.bulletFrequency;
    }
    if (_dire)
        return;
    var _t = setTimeout(function () {
        timer = undefined;
    }, 1000 / frequency);
    timer = _t;
};
